<?php
/**
 * Add by zhao.huang 20180201
 * 功能：处理商品打标图片业务相关内容
 * 
 */
namespace backend\controllers;

use Yii;
use backend\models\UploadForm;
use yii\web\AdminController;
use backend\models\GoodsMarkingImg;
use backend\models\GoodsSaleEdit;

class MarkingImgController extends AdminController
{
    public $layout = false;
    //业务关键字
    public $service = 'MarkingImg';
    
    public function actionIndex()
    {
        $model = new UploadForm();
        return $this->render('index',['model'=>$model]);
    }
    
    public function actionGetMarkingImg(){
        $data = Yii::$app->request->post();
        $mark_img = GoodsMarkingImg::find()->where('use_flg='.GoodsMarkingImg::STATUS_ENABLE);
        $count = $mark_img->count();
        $sort_string = empty($data['columns'][$data['order'][0]['column']]['data']) ? 'id' : $data['columns'][$data['order'][0]['column']]['data'];
        $mark_img = $mark_img->offset($data['start'])->limit($data['length'])->orderBy([
            $sort_string=>$data['order'][0]['dir'] == 'asc' ? SORT_ASC : SORT_DESC])->asArray()->all();
        $result['draw'] = intval($data['draw']);
        $result['recordsTotal'] = intval($count);
        $result['recordsFiltered'] = intval($count);
        $result['data'] = $mark_img;
        return \yii\helpers\Json::encode($result);
    }
    
    public function actionMarkingImgDetail(){
        $data = Yii::$app->request->post();
        $id = $data['id'];
        $mark_img = GoodsMarkingImg::find()->select(
            'id,sort,img_name,img_url'
        )->where('id=:id',[':id'=>$id])->asArray()->one();
        $this->retJson($mark_img,200);
    }
    
    public function actionChangeMarkingImgStatus(){
        $model = new GoodsMarkingImg();
        $data = Yii::$app->request->post();
        $id = $data['id'];
        $use_flg = $data['use_flg'];
        $goods_sale = GoodsSaleEdit::find()->where('marking_img_id=:marking_img_id',[':marking_img_id'=>$id])->count();
        if ($goods_sale > 0){
            $this->retJson('已有商品使用此标签，无法删除，请确认',500);
        }
        $data_new = [
            'use_flg'=>$use_flg,
            'editor'=>$model->getCreator(),
            'edit_time'=>date('Y-m-d H:i:s')
        ];
        $ret = $model::updateAll($data_new,['id'=>$id]);
        if(!$ret){
            $this->retJson('操作失败',500);
        }
        $this->retJson('操作成功',200);
    }
    
    public function actionChangeMarkingImg(){
        $marking_img = new GoodsMarkingImg();
        $data = Yii::$app->request->post();
        try {
            $img_name = trim($data['img_name']);
            $img_url = trim($data['img_url']);
            $sort = intval($data['sort']);
            $marking_img_id = $data['marking_img_id'];
            $creator = $marking_img->getCreator();
            $oper_time = date('Y-m-d H:i:s');
            if (!$img_name){
                throw new \Exception('请填写图片名称');
            }
            if (!$img_url){
                throw new \Exception('请上传商品打标图片');
            }
            if ($marking_img_id){
                $marking_img = GoodsMarkingImg::findOne($marking_img_id);
                $marking_img->editor = $creator;
                $marking_img->edit_time = $oper_time;
            }else{
                $marking_img->creator = $creator;
                $marking_img->create_time = $oper_time;
                $marking_img->use_flg = $marking_img::STATUS_ENABLE;
            }
            $marking_img->img_name = $img_name;
            $marking_img->img_url = $img_url;
            $marking_img->sort = $sort ? $sort : 0;
            $marking_img->save();
            $id = $marking_img->attributes['id'];
            if (!$id){
                throw new \Exception('操作失败，请重试');
            }
            $this->retJson('编辑成功',200);
        } catch (\Exception $e) {
            error_log('error->' . __METHOD__ . ' error:' . $e->getMessage());
            $this->retJson($e->getMessage(),500);
        }
    }
}